extends Node2D

class_name World

@export var chairs: Array[Chair] = []
var start = false
var score:= 0

const appearances = [
	preload("res://scenes/human/human_type/male_customer.tres"), 
	preload("res://scenes/human/human_type/female_customer.tres"), 
	preload("res://scenes/human/human_type/child_customer.tres"), 
]

const human_tmpl = preload("res://scenes/human/Human.tscn")

@onready var map:TileMap = $Ground
@onready var entry: Marker2D = $Ground/Marker2D
@onready var counter = $ui/HSplitContainer/Counter

# Called when the node enters the scene tree for the first time.
func _ready():
	add_to_group("play")

func getEmptyChair():
	for chair:Chair in chairs: 
		if chair.empty: 
			return chair
	return null

func _on_timer_timeout():
	if not start:
		return
	var human = human_tmpl.instantiate()
	var appear = appearances[randi() % 3]
	human.Appearance = appear
	map.add_child(human)
	human.position = entry.position
	var chair = getEmptyChair() as Chair
	if not chair: 
		human.queue_free()
		return 
	human.naviTo(chair.position)
	chair.empty = false
	
var on_menu = false	

func menu():
	get_tree().paused = true
	
@onready var panel = $ui/HSplitContainer/Panel	
	
func _input(event):
	if event.is_action_pressed("k_start"):
		start = true
	if event.is_action_pressed("k_menu"):
		pass
	if event.is_action_pressed("k_title"):
		if panel.visible:
			panel.hide()
		else: 
			panel.show()


func add_score():
	score += 100
	print("add score ", score)
	counter.text = str(score)

func _on_chair_add_score():
	add_score()

func _on_event_timer_timeout():
	pass
	
